package javafx.officialdemo.ChoiceBox;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * 下拉列表
 */
public class FXChoiceBox extends Application {
	/**
	 * 构造器
	 * 
	 * ChoiceBox cb = new ChoiceBox(FXCollections.observableArrayList("First",
	 * "Second", "Third"));
	 * 
	 * 带分割线 ChoiceBox cb = new ChoiceBox();
	 * cb.setItems(FXCollections.observableArrayList( "New Document", "Open ",
	 * new Separator(), "Save", "Save as") );
	 */

	/**
	 * 增加监听事件 增加Tooltip
	 */
	public ChoiceBox test() {
		ChoiceBoxItemObject[] items = new ChoiceBoxItemObject[3];
		items[0] = new ChoiceBoxItemObject(1, "111");
		items[1] = new ChoiceBoxItemObject(2, "222");
		items[2] = new ChoiceBoxItemObject(3, "333");
		
		ChoiceBox cb = new ChoiceBox();
		ObservableList list = FXCollections.observableArrayList(items);
		cb.setItems(list);
		
		
		SingleSelectionModel selectionModel = cb.getSelectionModel();
		selectionModel.selectedItemProperty().addListener(new ChangeListener() {
			public void changed(ObservableValue arg0, Object arg1, Object arg2) {
				
				ChoiceBoxItemObject selectObj = (ChoiceBoxItemObject)arg2;
				
				System.out.println(selectObj.no+"->"+selectObj);
				
			}
		});
		Tooltip tooltip = new Tooltip("ChoiceBox");
		cb.setTooltip(tooltip);

		return cb;
	}

	public void start(Stage primaryStage) throws Exception {
		GridPane root = new GridPane();

		ChoiceBox test = test();
		root.add(test, 1, 1);

		Scene scene = new Scene(root, 300, 250);
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public static void main(String[] args) {
		launch(null);
	}
}

class ChoiceBoxItemObject{
	public int no = 0;
	public String desc = null;
	
	public ChoiceBoxItemObject(int no, String desc) {
		super();
		this.no = no;
		this.desc = desc;
	}

	public String toString() {
		return desc;
	}
}
